package net.jcrq.HamSatDroid;

import android.app.AlertDialog;
import android.content.Context;
import android.os.Bundle;
import android.preference.EditTextPreference;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceChangeListener;
import android.preference.PreferenceActivity;
import android.preference.PreferenceScreen;
import android.text.InputType;

public class PrefHandling extends PreferenceActivity implements HSDConstants {

    private static final String HOME_LON = "homeLon";
    private static final String MAIN_PREFERENCES = "main_preferences";
    private static final String HOME_LAT = "homeLat";
    private Context context;
    private EditTextPreference latPref;
    private EditTextPreference lonPref;

    @Override
    protected void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Load the XML preferences file
        addPreferencesFromResource(R.xml.preference_root);

        latPref = new EditTextPreference(this);
        latPref.setKey(HOME_LAT);
        latPref.setTitle("Latitude");
        latPref.setDefaultValue(ZERO_STRING);
        latPref.setDialogMessage("Please enter latitude in degrees, must be between -90 and 90");
        ((PreferenceScreen)findPreference(MAIN_PREFERENCES))
                .addPreference(latPref);

        lonPref = new EditTextPreference(this);
        lonPref.setKey(HOME_LON);
        lonPref.setTitle("Longitude");
        lonPref.setDefaultValue(ZERO_STRING);
        lonPref.setDialogMessage("Please enter longitude in degrees, must be between -180 and 180");
        ((PreferenceScreen)findPreference(MAIN_PREFERENCES))
                .addPreference(lonPref);

        // Save context for later
        context = this;

        latPref.getEditText().setInputType(
                InputType.TYPE_CLASS_NUMBER
                        | InputType.TYPE_NUMBER_FLAG_DECIMAL
                        | InputType.TYPE_NUMBER_FLAG_SIGNED);
        lonPref.getEditText().setInputType(
                InputType.TYPE_CLASS_NUMBER
                        | InputType.TYPE_NUMBER_FLAG_DECIMAL
                        | InputType.TYPE_NUMBER_FLAG_SIGNED);

        latPref.setOnPreferenceChangeListener(new LatLonPrefChangeListener());
        lonPref.setOnPreferenceChangeListener(new LatLonPrefChangeListener());

    }

    public class LatLonPrefChangeListener implements OnPreferenceChangeListener {

        @Override
        public boolean onPreferenceChange(final Preference changedPref,
                final Object newValue) {
            boolean acceptInput = false;

            final String newString = (String)newValue;
            String dialogString = "";

            if (changedPref.getKey().equals(HOME_LAT)) {
                acceptInput = PrefHandling.validateStringValueWithinBounds(
                        newString, 90);
                dialogString = "Invalid Latitude, set to 0";
            }
            if (changedPref.getKey().equals(HOME_LON)) {
                acceptInput = PrefHandling.validateStringValueWithinBounds(
                        newString, 180);
                dialogString = "Invalid Longitude, set to 0";
            }

            if (!acceptInput) {
                new AlertDialog.Builder(context).setMessage(dialogString)
                        .setPositiveButton("OK", null).show();
                if (changedPref.getKey().equals(HOME_LAT)) {
                    latPref.setText(ZERO_STRING);
                }
                if (changedPref.getKey().equals(HOME_LON)) {
                    lonPref.setText(ZERO_STRING);
                }
            }
            return acceptInput;
        }

    }

    private static boolean validateStringValueWithinBounds(final String value,
            final int bound) {
        boolean isValid = false;
        final double doubleValue = Double.valueOf(value);
        try {
            if ((doubleValue <= bound) && (doubleValue >= -bound)) {
                isValid = true;
            }
        }
        catch (final NumberFormatException e) {
            e.printStackTrace();
        }
        return isValid;
    }

}
